<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>...</title>
    <script>
        const url = new URL(window.location.href);
        const params = new URLSearchParams(url.search);
        const deviceId = params.get('device_id');

        let queryString = ''

        localStorage.setItem('device_id', deviceId)
        getConfig();


        async function getConfig() {
            const settingsRes1 = await fetch('http://18.167.187.79/config_h5_develop.json?' + new Date().getTime())
            const settingsRes2 = await settingsRes1.json()
            // console.error(settingsRes2)
            const settingsRes = await fetch(settingsRes2.chat_setting + '?' + new Date().getTime())
            const settings = await settingsRes.json()
            // console.error('settings', settings.data.info_url)
            if (deviceId) {
                // 获取角色
                fetch(settings.data.info_url + `/api/user/info?device_id=${deviceId}`).then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok ' + response.statusText);
                    }
                    return response.json(); // 将响应解析为 JSON
                }).then(res => {
                    if (res.data) {
                        console.error('res', res)
                        queryString = `device_id=${deviceId}`
                        queryString += '&' + Object.keys({
                            ...res.data,
                            ...getUrlParams(location.href)
                        })
                            .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(res.data[key])}`)
                            .join('&');

                        jump('?' + queryString)
                    } else {
                        jump('')
                    }
                })
            } else {
                jump('')
            }


            function jump(query = '') {

                if (isMobile()) {
                    console.log(settings.data.chat_jump_h5_url + query);

                    location.href = settings.data.chat_jump_h5_url + query
                } else {
                    location.href = settings.data.chat_jump_pc_url + query
                }
            }

        }



        function isMobile() {
            let flag = navigator.userAgent.match(
                /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
            );
            return flag;
        }

        function getUrlParams(url) {
            const params = {};
            try {
                const parser = new URL(url);

                for (const [key, value] of parser.searchParams.entries()) {
                    params[key] = value;
                }

                if (parser.hash.includes('?')) {
                    const hashParams = parser.hash.split('?')[1];
                    const pairs = hashParams.split('&');
                    for (const pair of pairs) {
                        const [key, value] = pair.split('=');
                        params[key] = decodeURIComponent(value);
                    }
                }
            } catch { }

            return params;
        }

    </script>
</head>

<body></body>

</html>